package com.orchid.shop.manager.controller;


import com.orchid.shop.manager.service.SysUserService;
import com.orchid.shop.manager.service.ValidateCodeService;
import com.orchid.shop.model.dto.system.LoginDto;
import com.orchid.shop.model.entity.system.SysUser;
import com.orchid.shop.model.vo.common.Result;
import com.orchid.shop.model.vo.common.ResultCodeEnum;
import com.orchid.shop.model.vo.system.LoginVo;
import com.orchid.shop.model.vo.system.ValidateCodeVo;
import com.orchid.shop.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Tag(name ="用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;


    //用户退出
    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    //获取当前登录用户信息
//    @GetMapping("/getUserInfo")
//    public Result getUserInfo(HttpServletRequest request) {
////    public Result getUserInfo(@RequestHeader(name = "token") String token ) {  //第二种方式
//        //从请求头里面获取token
//        String token = request.getHeader("token");
//
//        //根据token查询redis获取用户信息
//        SysUser sysUser = sysUserService.getUserInfo(token);
//
//        //用户信息返回
//        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }


    @GetMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }



    //生成图片验证码
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    //用户登录
    @Operation(summary = "登录接口")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
}
